﻿Public Class frmTiepNhanMathang


    Private Sub frmTiepNhanMathang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDanhSachLenLuoi()
        LoadTenDonVITinh()
        LoadTenNhaCungCap()
    End Sub
    Public Sub LoadTenDonVITinh()
        cmbTenDonVi.DataSource = DonViTinhDAO.selectALL()
        cmbTenDonVi.DisplayMember = "TENDONVITINH"
        cmbTenDonVi.ValueMember = "MADONVITINH"
    End Sub
    Public Sub LoadTenNhaCungCap()
        cmbTenNhaCungCap.DataSource = NhaCungCapDAO.selectALL()
        cmbTenNhaCungCap.DisplayMember = "TENNHACUNGCAP"
        cmbTenNhaCungCap.ValueMember = "MANHACUNGCAP"
    End Sub
    Public Sub LoadDanhSachLenLuoi()
        dgvDanhSachMatHang.DataSource = MatHangDAO.selectall()
    End Sub
    Public Sub HienThi()
        If dgvDanhSachMatHang.SelectedRows.Count > 0 Then
            txtMaMatHang.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(0).Value.ToString()
            txtTenMatHang.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(1).Value.ToString()
            txtDonGiaSi.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(2).Value.ToString()
            txtDonGiaLe.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(3).Value.ToString()
            cmbTenDonVi.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(4).Value.ToString()
            txtSoLuongTon.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(5).Value.ToString()
            cmbTenNhaCungCap.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(6).Value.ToString()
        End If
    End Sub

    Private Sub dgvDanhSachMatHang_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachMatHang.SelectionChanged
        HienThi()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim MH As New MatHangDTO
        MH.MaMatHang = txtMaMatHang.Text
        MH.TenMatHang = txtTenMatHang.Text
        MH.SoLuongTon = txtSoLuongTon.Text
        MH.MaDonViTinh = cmbTenDonVi.SelectedValue
        MH.MaNhaCungCap = cmbTenNhaCungCap.SelectedValue
        MH.DonGiaLe = txtDonGiaLe.Text
        MH.DOnGiaSi = txtDonGiaSi.Text
        MatHangDAO.insert(MH)
        LoadDanhSachLenLuoi()
    End Sub

    Private Sub BtnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnXoa.Click
        Dim Ma As String
        Ma = txtMaMatHang.Text
        MatHangDAO.delete(Ma)
        LoadDanhSachLenLuoi()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim MH As MatHangDTO = New MatHangDTO
        MH.MaMatHang = txtMaMatHang.Text
        MH.MaDonViTinh = cmbTenDonVi.SelectedValue
        MH.MaNhaCungCap = cmbTenNhaCungCap.SelectedValue
        MH.SoLuongTon = txtSoLuongTon.Text
        MH.TenMatHang = txtTenMatHang.Text
        MH.DonGiaLe = txtDonGiaLe.Text
        MH.DOnGiaSi = txtDonGiaSi.Text
        MatHangDAO.Update(MH)
        LoadDanhSachLenLuoi()
    End Sub
End Class